#!/usr/bin/env python
"""
Nagios plugin that checks the overal usage of the grid pool accounts.
The plugin reports as a performance measurement, the number and ratio of the pool
account that with the highest usage pattern. (I.e. if there are various pool accounts,
the plugin only reports on the max used one.)

Performance metric: max_pool=ratio(used/available)%;warn;crit

-c : critical pool usage threshold. Ratio range [ 0.0 - 1.0] (0.9 default)
-w : warning pool usage threshold. Ratio range in [0.0 - 1.0] (0.7 default)

(no check whether c values <= w values)

"""
import sys 
from nagios_plugin import NagiosPluginBasic, RETURN_CODE
from arc_conf_parser import *

__author__ = "Placi Flury grid@switch.ch"
__date__ = "18.05.10"
__version__ = "0.2.1"


class SupportedVOs(NagiosPluginBasic):
    """ Probing of enabled pools of grid user accounts. The plugin
        returns the usage of each individual pool. The ratio of the 
        maximally used pool is reported as performance data.
    """
    def __init__(self):
        NagiosPluginBasic.__init__(self)
        self.options = None
        self.getOptions()
        try:
            self.parser = ARC_Parser(self.options.arc_conf)
            self.parser.set_group2vo_map()

        except ParseError, e1:
            print "Error: %s" % e1.message
            sys.exit(RETURN_CODE["critical"])
        except SanityError, e2:
            print "Error: %s" % e2.message
            sys.exit(RETURN_CODE["critical"])
        
    
    def getOptions(self):
        """ command-line options parser """
        parser = NagiosPluginBasic.getOptions(self)
        parser.remove_option("-H") # remove hostname option
        parser.remove_option("-c")
        parser.remove_option("-w")
        parser.add_option("", "--arc_conf", action="store",
                    dest="arc_conf", default="/etc/arc.conf",
                    help="Location of the arc.conf file. (default:%default)")

        self.options  = parser.parse_args()[0]
        
    

    def get_vos(self):
        """ probe for enabled pool accounts and their current usage. """
        vos = list()
        for vo in  self.parser.get_group2vo_map().values(): # eliminate duplicates
            if vo not in vos:
                vos.append(vo)

        print "OK: %s|vos=%s" % (vos, len(vos))
        sys.exit(RETURN_CODE["ok"])
 
    def main(self):
        self.get_vos()
        

if __name__ == "__main__":
    gp = SupportedVOs()
    gp.main()


